package com.gmzg.gmzgoffice.works.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gmzg.gmzgoffice.R;
import com.gmzg.gmzgoffice.entity.Approval;

import java.util.Collection;
import java.util.List;
import java.util.zip.Inflater;

/**
 * author: GD on 2016/11/9 10:26
 * email :2301722691@qq.com
 */

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> implements View.OnClickListener {

    private List<Approval> mList;
    private Context mContext;
    private LayoutInflater mInflater;
    private OnChildClickListener mListener;
    private RecyclerView mRecyclerView;
    private String mUserName;
    public RecyclerAdapter(List<Approval> list, Context context,String username) {
        mList = list;
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mUserName = username;
    }

    public void setOnChildClickListener(OnChildClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View v) {
        if (mRecyclerView != null && mListener != null) {
            int position = mRecyclerView.getChildAdapterPosition(v);
            mListener.onChildClick(mRecyclerView, v, position, mList.get(position));
        }
    }

    public interface OnChildClickListener{
        void onChildClick(RecyclerView parent,View view,int position,Approval data);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.approve_item, parent, false);
        itemView.setOnClickListener(this);
        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Approval approval = mList.get(position);
        String approvaer = approval.getWho_approval();
        holder.mTitle.setText(approval.getApproval_type()+"单");
        if (approval.getApproval_Status()){
            if (approvaer.equals(mUserName)){
                holder.mApproveText.setText(approvaer +"已审批");
            }else {
                holder.mApproveText.setText("已审批");
            }
        }else {
            if (approvaer.equals(mUserName)){
                holder.mApproveText.setText("待审批");
            }else {
                holder.mApproveText.setText("等待"+approvaer+"审批");
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        mRecyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        mRecyclerView = null;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder  {
        private final TextView mApproveText;
        private TextView mTime;
        private TextView mTitle;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.item_title_tv);
            mTime = (TextView) itemView.findViewById(R.id.item_time_tv);
            mApproveText = (TextView)itemView.findViewById(R.id.item_approve_tv);
        }


    }

    public void addList(Collection<? extends  Approval> list){
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }
}
